/*
 * Copyright (C) 2009 Rafael Fernandes
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.phonebooksharing.services.servlets;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.PrintWriter;

import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.phonebooksharing.dto.dashboard.DashboardReaderItem;
import com.phonebooksharing.dto.friend.ContactItem;
import com.phonebooksharing.services.ejb3.facade.DashboardFacade;

/**
 * Servlet implementation class DashboardServlet
 */
@SuppressWarnings("unchecked")
public class DashboardServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public DashboardServlet() {
        super();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	    PrintWriter pw = response.getWriter();
        response.setContentType("text/html");
        pw.print("<html><head/><body><h1>Welcome to Phonebook Sharing Services!</h1><br/><h3>You should be posting here!</h3></body></html>");
	}

	@EJB
	private DashboardFacade dashboardFacade;
	
	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	    try {
            ObjectInputStream ois = new ObjectInputStream(request.getInputStream());
            
            final String ac = request.getParameter("ac");
            if("u".equalsIgnoreCase(ac)) {//update dashboard
                final String authItem = (String)ois.readObject();//should always be this                
                ObjectOutputStream oos = new ObjectOutputStream(response.getOutputStream());
                oos.writeObject( dashboardFacade.updateDashboard(authItem) );
                
            } else if("gc".equalsIgnoreCase(ac)) {//get contacts
                final int page     = new Integer(request.getParameter("p"));
                final int pageSize = new Integer(request.getParameter("ps"));
                
                final String authItem = (String)ois.readObject();//should always be this
                ObjectOutputStream oos = new ObjectOutputStream(response.getOutputStream());
                oos.writeObject( dashboardFacade.getDashboardContacts(authItem, page, pageSize) );
            } else if("m".equalsIgnoreCase(ac)) {//get messages
                final int type     = new Integer(request.getParameter("q"));
                final int page     = new Integer(request.getParameter("p"));
                final int pageSize = new Integer(request.getParameter("ps"));
                
                final String authItem = (String)ois.readObject();//should always be this
                ObjectOutputStream oos = new ObjectOutputStream(response.getOutputStream());
                oos.writeObject( dashboardFacade.getDashboardMessages(authItem, type, page, pageSize) );
                
            } else if("p".equalsIgnoreCase(ac)) {//images
                final int page     = new Integer(request.getParameter("p"));
                final int pageSize = new Integer(request.getParameter("ps"));
                
                final String authItem = (String)ois.readObject();//should always be this
                ObjectOutputStream oos = new ObjectOutputStream(response.getOutputStream());
                oos.writeObject( dashboardFacade.getDashboardImages(authItem, page, pageSize) );
                
            } else if("v".equalsIgnoreCase(ac)) {//images
                final int page     = new Integer(request.getParameter("p"));
                final int pageSize = new Integer(request.getParameter("ps"));
                
                final String authItem = (String)ois.readObject();//should always be this
                ObjectOutputStream oos = new ObjectOutputStream(response.getOutputStream());
                oos.writeObject( dashboardFacade.getDashboardVideos(authItem, page, pageSize) );
                
            } else if("pc".equalsIgnoreCase(ac)) {//postcards
                final int page     = new Integer(request.getParameter("p"));
                final int pageSize = new Integer(request.getParameter("ps"));
                
                final String authItem = (String)ois.readObject();//should always be this
                ObjectOutputStream oos = new ObjectOutputStream(response.getOutputStream());
                oos.writeObject( dashboardFacade.getDashboardPostcards(authItem, page, pageSize) );
                
            } else if("rm".equalsIgnoreCase(ac)) {//response messages
                final DashboardReaderItem<ContactItem> item = (DashboardReaderItem<ContactItem>)ois.readObject();//should always be this
                
                ObjectOutputStream oos = new ObjectOutputStream(response.getOutputStream());
                oos.writeObject( dashboardFacade.createTextResponse(item) );
                
            } 
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
	}
}
